//
//  GBAddFriendAnimation.m
//  弹出框动画
//
//  Created by guobin on 15/10/1.
//  Copyright © 2015年 郭斌. All rights reserved.
//

#import "GBAddFriendAnimation.h"
#import <QuartzCore/QuartzCore.h>
#import "GBButton.h"

@interface GBAddFriendAnimation ()<ButtonClick>
{
    //显示的view
    UIView *mainView;

}
@end

@implementation GBAddFriendAnimation

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
//        _titleArray = @[@"筛选", @"设置"];
//        _imageArray = @[@"筛选", @"监测设置"];
        self.backgroundColor = [UIColor colorWithHue:0 saturation:0 brightness:0 alpha:0];
        self.frame = CGRectMake(0, 0, screenW, screenH);
        //声明一个button，也可以用点击手势来代替
        UIButton *tapButton = [UIButton buttonWithType:0];
        tapButton.frame = CGRectMake(0, 0, screenW, screenH);
        tapButton.backgroundColor = [UIColor clearColor];
//        tapButton.alpha = 0;
        [tapButton addTarget:self action:@selector(hideAnimation) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:tapButton];
        
        /*
         * 在这里的mainview是具体显示内容的view
         * 需要在使用的时候，安装项目需求，单独定制
         * 在这里做的只是，优化弹出界面的动画
         */
        //创建小的显示的view
        mainView = [[UIView alloc] initWithFrame:frame];
        [self createMainView];
        
        //弹出时的动画
        [self showAnimaton];
    }
    return self;
}
- (void)setImageArray:(NSArray *)imageArray{
    _imageArray = imageArray;
    //初始化按钮
    [self createMainButton];
}
#pragma mark - 创建小的显示的view
-(void)createMainView
{
    mainView.backgroundColor = KDColor;
    mainView.layer.cornerRadius = 5;
    [self addSubview:mainView];
    
//    //初始化按钮
//    [self createMainButton];
}

#pragma mark - 初始化功能按钮
-(void)createMainButton
{
    for(int i = 0; i < (int)_titleArray.count; i++)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 15 + 40 * i, 20, 20)];
        imageView.image = [UIImage imageNamed:_imageArray[i]];
        
        GBButton *btn = [[GBButton alloc] initWithFrame:CGRectMake(40, 5 + 40 * i, 50, 40) withTitle:_titleArray[i]];
        btn.tag = 1000 + i;
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        btn.delegate100 = self;
        [mainView addSubview:btn];
        [mainView addSubview:imageView];
        
        if(i > 0)
        {
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5 + 40 * i, 80, 0.25)];
            label.backgroundColor = [UIColor lightGrayColor];
            [mainView addSubview:label];
        }
    }
}

#pragma mark - 按钮点击的代理方法
-(void)btnClick:(UIButton *)sender
{
    //界面消失
    [self hideAnimation];
    //将点击结果传递给控制器
    [self.delegate1001 btnClickToController:sender];
    
}

#pragma mark - 画箭头
-(void)drawRect:(CGRect)rect
{
    UIBezierPath *arrowPath = [UIBezierPath bezierPath];
    arrowPath.lineCapStyle = kCGLineCapRound; //线条拐角
    arrowPath.lineJoinStyle = kCGLineCapRound; //终点处理
    [KDColor setFill];
    
    [arrowPath moveToPoint:    (CGPoint){screenW -15, mainView.y }];
    [arrowPath addLineToPoint: (CGPoint){screenW - 27, mainView.y  -12}];
    [arrowPath addLineToPoint: (CGPoint){screenW -39, mainView.y }];
    [arrowPath addLineToPoint: (CGPoint){screenW -15, mainView.y }];
    [arrowPath closePath];
    [arrowPath fill];
}

#pragma mark - 弹出时的动画
-(void)showAnimaton
{
    
    //弹出动画
    [UIView animateWithDuration:0.2 animations:^{
        
        self.alpha = 1;
    } completion:nil];
}

#pragma mark - 当前界面消失
-(void)TapDisappear
{
    [mainView removeFromSuperview];
    mainView = nil;
    [self removeFromSuperview];
}

#pragma mark - 当前界面消失
-(void)hideAnimation
{
    //消失动画
    [UIView animateWithDuration:0.38 animations:^{
        
        self.alpha = 0;
    } completion:^(BOOL finished){
        
        [self TapDisappear];
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
